#include "hash.h"
#include "sha256.h"

//sha1_context g_ctxSha1;
// static sha256_context g_ctxSha256;
static sha256_t g_ctxSha256;

void debug_info(char *string, unsigned char *data, int len)
{
    // printf("%s-%d.\n", string, len);
    // for (int i = 0; i < len; i++)
    // {
    //     printf("0x%02x, ", data[i]);
    //     if ((i+1)%16==0)
    //         printf("\n");
    // }
    // printf("\n");
}

int smHash(char bHashAlg, unsigned char *pbData, int wDataLen, unsigned char *pbResult, char bMode)
{
	if (bMode == HASH_MODE_INIT)
	{
		if (bHashAlg == HASH_ALG_SHA1)
		{
			//sha1_starts(&g_ctxSha1);
		}
		else if (bHashAlg == HASH_ALG_SHA256)
		{
			// sha256_starts(&g_ctxSha256, 0);
			sha256_init(&g_ctxSha256);
		}
	}
	else if (bMode == HASH_MODE_UPDATE)
	{
		if (bHashAlg == HASH_ALG_SHA1)
		{
			//sha1_update(&g_ctxSha1, pbData, wDataLen);
		}
		else if (bHashAlg == HASH_ALG_SHA256)
		{
			sha256_update(&g_ctxSha256, pbData, wDataLen);
		}
	}
	else if (bMode == HASH_MODE_FINAL)
	{
		if (bHashAlg == HASH_ALG_SHA1)
		{
			//sha1_finish(&g_ctxSha1, pbResult);
		}
		else if (bHashAlg == HASH_ALG_SHA256)
		{
			sha256_final(&g_ctxSha256, pbResult);
		}
	}
	else
	{
		return -1;
	}

	return 0;
}

int smHashData(char bHashAlg, unsigned char *pbData, int wDataLen, unsigned char *pbResult)
{
	if (bHashAlg == HASH_ALG_SHA1)
	{
		//sha1(pbData, wDataLen, pbResult);
	}
	else if (bHashAlg == HASH_ALG_SHA256)
	{
		// sha256(pbData, wDataLen, pbResult, 0);
		sha256_hash(pbResult, pbData,(size_t)wDataLen);
	}

	return 0;
}

int sm_sha256_init(void)
{
	smHash(HASH_ALG_SHA256, NULL, 0, NULL, HASH_MODE_INIT);
    return 0;
}

int sm_sha256_update(unsigned char *dat, unsigned int len)
{
    if (NULL == dat)
    {
        return -1;
    }

	smHash(HASH_ALG_SHA256, dat, len, NULL, HASH_MODE_UPDATE);
    return 0;
}

int sm_sha256_final(unsigned char *signature)
{
	smHash(HASH_ALG_SHA256, NULL, 0, signature, HASH_MODE_FINAL);
    return 0;
}

